﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calc
{
    public partial class formCalc : Form
    {
        int coma = 0;
        int act = 0;
        int equ = 0;
        double buffer = 0;
        const int maxCount = 16;

        myCalc calc = new myCalc();

        public formCalc()
        {
            InitializeComponent();            
        }

        private void reset()
        {
            act = 0;
            coma = 0;
            equ = 0;
            txtbNumber.Text = "0";
            calc.reset();
        }

        private void myFocus()
        {
            txtbNumber.Focus();
            SendKeys.Send("{END}");
        }
        private void btnEqual_Click(object sender, EventArgs e)
        {
            myFocus();
            if (act != 0)
            {
                if (equ == 0)
                {
                    calc.setSecond(buffer);
                }
                    txtbNumber.Text = calc.countUp();
                    equ = 1;                
            }
            act = 1;
        }

        private void txtbNumber_TextChanged_1(object sender, EventArgs e)
        {
            if (txtbNumber.Text == "")
            {
                txtbNumber.Text = "0";
                return;
            }
            if (buffer == 0)
            if (coma == 0)
                if (txtbNumber.Text[0] == '0')
                {
                    string tmp = "";
                    for (int i = 1; i < txtbNumber.Text.Length; i++)
                        tmp += txtbNumber.Text[i];
                    txtbNumber.Text = tmp;
                }
            
            try
            {
                double tmp = double.Parse(txtbNumber.Text);
                buffer = tmp;
            
            
                if (txtbNumber.Text.Length >= 17)
                    txtbNumber.Text = txtbNumber.Text.Remove(16);
                myFocus();
            }
            catch
            {
                txtbNumber.Text = buffer.ToString();
            }
        }

        private void ChangeText(string s)
        {
            myFocus();
            if (act == 1)
            {
                act = 2;
                txtbNumber.Text = "0";
                coma = 0;
            }
            if (equ == 1)
            {
                reset();
            }
            if (txtbNumber.Text.Length >= maxCount) return;
            if (txtbNumber.Text == "0")
                if (coma == 0)
                {
                    txtbNumber.Text = s;
                    return;
                }
            if (coma == 1)
            {
                txtbNumber.Text += "," + s;
                coma = 2;
            }
            else txtbNumber.Text += s;
        }

        private void btnComa_Click(object sender, EventArgs e)
        {
            myFocus();
            if (coma == 0)
                coma = 1;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (coma == 0)
                if (txtbNumber.Text == "0")
                    return;
            ChangeText("0");
        }

        private void btm00_Click(object sender, EventArgs e)
        {
            if (coma == 0)
                if (txtbNumber.Text == "0")
                    return;
            ChangeText("00");
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            ChangeText("1");
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            ChangeText("2");
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            ChangeText("3");
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            ChangeText("4");
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            ChangeText("5");
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            ChangeText("6");
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            ChangeText("7");
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            ChangeText("8");
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            ChangeText("9");
        }

        private void btnMp_Click(object sender, EventArgs e)
        {
            myFocus();
            calc.memoryPlus(buffer);
            txtbMemory.Text = "M";
        }

        private void btnMm_Click(object sender, EventArgs e)
        {
            myFocus();
            calc.memoryMines(buffer);
            txtbMemory.Text = "M";
        }

        private void btnMS_Click(object sender, EventArgs e)
        {
            myFocus();
            calc.memorySave(buffer);
            txtbMemory.Text = "M";
        }

        private void btnMR_Click(object sender, EventArgs e)
        {
            myFocus();
            if (txtbMemory.Text == "M")
                txtbNumber.Text = calc.memoryReload();
        }

        private void btnMC_Click(object sender, EventArgs e)
        {
            myFocus();
            calc.memoryClear();
            txtbMemory.Text = " ";
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            myFocus();
            reset();
        }

        private void btnPM_Click(object sender, EventArgs e)
        {
            myFocus();
            if (txtbNumber.Text[0] != '-')
                txtbNumber.Text = "-" + txtbNumber.Text;
            else
            {
                string tmp = "";
                for (int i = 1; i < txtbNumber.Text.Length; i++)
                    tmp += txtbNumber.Text[i];
                txtbNumber.Text = tmp;
            }
            calc.setFirst(buffer);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            myFocus();
            txtbNumber.Text = txtbNumber.Text.Remove(txtbNumber.Text.Length - 1);
            if (txtbNumber.Text[txtbNumber.Text.Length - 1] == ',')
            {
                txtbNumber.Text = txtbNumber.Text.Remove(txtbNumber.Text.Length - 1);
                coma = 0;
            }
        }

        private void brnSqrt_Click(object sender, EventArgs e)
        {
            myFocus();
            calc.setFirst(buffer);
            txtbNumber.Text = calc.sqrt();
        }

        private void btnRP_Click(object sender, EventArgs e)
        {
            myFocus();
            calc.setFirst(buffer);
            txtbNumber.Text = calc.reciproc();
        }

        private void setAction(char c)
        {
            equ = 0;
            if (act == 0)
            {
                calc.setFirst(buffer);
                act = 1;
                calc.setAction(c);
            }
            else if (act == 1)
            {
                calc.setAction(c);
            }
            else if (act == 2)
            {
                calc.setSecond(buffer);
                txtbNumber.Text = calc.countUp();
                calc.setAction(c);
                act = 1;
            }
        }
        private void btnPlus_Click(object sender, EventArgs e)
        {
            myFocus();
            setAction('+');
        }

        private void btnMines_Click(object sender, EventArgs e)
        {
            myFocus();
            setAction('-');
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            myFocus();
            setAction('*');
        }

        private void btnDevide_Click(object sender, EventArgs e)
        {
            myFocus();
            setAction('/');
        }

        private void btnFocus_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            messageBoxCS.AppendFormat("{0} = {1}", "Alt", e.Alt);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "Control", e.Control);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "KeyCode", e.KeyCode);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "KeyValue", e.KeyValue);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "KeyData", e.KeyData);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "Modifiers", e.Modifiers);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "Shift", e.Shift);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "IsInputKey", e.IsInputKey);
            messageBoxCS.AppendLine();
            MessageBox.Show(messageBoxCS.ToString(), "PreviewKeyDown Event");
        }

        private void formCalc_Shown(object sender, EventArgs e)
        {
            ChangeText("1");
            txtbNumber.Text = "0";
            SendKeys.Send("{END}");
        }

        private void txtbNumber_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {

            //+
            if (e.KeyCode == Keys.Oemplus && e.Shift || e.KeyCode == Keys.Add)
                btnPlus_Click(sender, e);
            //-
            if (e.KeyCode == Keys.OemMinus && !e.Shift || e.KeyCode == Keys.Subtract)
                btnMines_Click(sender, e);
            //*
            if (e.KeyCode == Keys.D8 && e.Shift || e.KeyCode == Keys.Multiply)
                btnMultiply_Click(sender, e);
            // /
            if (e.KeyCode == Keys.OemQuestion && !e.Shift || e.KeyCode == Keys.Divide)
                btnDevide_Click(sender, e);
            // esc
            if (e.KeyCode == Keys.Escape)
                btnC_Click(sender, e);
            if (e.KeyCode == Keys.Back)
                btnBack_Click(sender, e);
        }

        private void txtbNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            //+
            if (e.KeyChar == '+')
                btnPlus_Click(sender, e);
            //-
            if (e.KeyChar == '-')
                btnMines_Click(sender, e);
            //*
            if (e.KeyChar == '*')
                btnMultiply_Click(sender, e);
            // /
            if (e.KeyChar == '/')
                btnDevide_Click(sender, e);
            // esc
            if ((int)e.KeyChar == 27)
                btnC_Click(sender, e);
            // back
            if ((int)e.KeyChar == 8)
                btnBack_Click(sender, e);
            if ((int)e.KeyChar == 13)
                btnEqual_Click(sender, e);
            if (e.KeyChar == '0')  btn0_Click(sender, e);
            if (e.KeyChar == '1')  btn1_Click(sender, e);
            if (e.KeyChar == '2')  btn2_Click(sender, e);
            if (e.KeyChar == '3')  btn3_Click(sender, e);
            if (e.KeyChar == '4')  btn4_Click(sender, e);
            if (e.KeyChar == '5')  btn5_Click(sender, e);
            if (e.KeyChar == '6')  btn6_Click(sender, e);
            if (e.KeyChar == '7')  btn7_Click(sender, e);
            if (e.KeyChar == '8')  btn8_Click(sender, e);
            if (e.KeyChar == '9')  btn9_Click(sender, e);            
        }       
    }
}
